import Vue from 'vue'
import Vuex from 'vuex'
import * as types from './types'
import VueCookies from 'vue-cookies'
Vue.use(Vuex)

const store = new Vuex.Store({
    state: {
        user: {gameName:""},
        token: null,
        power: {},
    },
    mutations: {
        [types.LOGIN]: (state, data) => {
            state.token = data.token;
            state.user = data.user;
            state.power = data.power;

            VueCookies.set("token",data.token);
            localStorage.user = JSON.stringify(data.user);
            localStorage.power = JSON.stringify(data.power);
        },
        [types.LOGOUT]: (state) => {
            VueCookies.remove("token");
            localStorage.removeItem('user');
            localStorage.removeItem('power');
            state.token = null;
            state.user = {gameName:""};
            state.power = {};
        },
    }
})

export default store